'<template>
    <div class="login_container">
        <div class="login_box">
            <!-- LOGO区 -->
         <div class="avatar_box">
        <img src="../assets/logo.gif" alt="" />
        </div>
            <!-- 登录表单 -->
            <el-form :model="loginForm" :rules="formRules" ref="ruleFormRef" label-width="100px" class="login_form">
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="用户名"></el-input>
                </el-form-item>
                <!-- 密码 -->
                 <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-search" placeholder="密码" autocomplete="off"></el-input>
                </el-form-item>
                <!-- 登录重置按钮 -->
                <el-form-item class="btns">
                    <el-button type="primary" @click="submitLoginForm">登录</el-button>
                    <el-button @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    //组件中的data是通过函数返回的对象
    data(){
        return {
           // 最终提交给后端的实体对象 var user = { username: "admin", password:"123"}
           loginForm:{
             username:'',
             password:''
           },
           // 表单验证规则
           formRules:{
              username: [
                { required: true, message: '请输入登录用户名', trigger: 'blur' },
                { min: 5, max: 15, message: '用户名长度在 5 到 15 个字符', trigger: 'blur' }
             ],
             password: [
                { required: true, message: '请输入登录密码', trigger: 'blur' },
                { min: 6, max: 15, message: '密码长度在 6 到 15 个字符', trigger: 'blur' }
             ]
           }
        }
    },
    methods:{
        // 重置按钮事件
        resetLoginForm(){
           this.$refs.ruleFormRef.resetFields();
        },
        // 提交登录函数
        submitLoginForm(){
            //简化promise写法
           this.$refs.ruleFormRef.validate(async validate => {
             if(validate){
                // 提交,通过axios异步方式与后台进行交互
                const {data:res} = await this.$http.post("suclogin",this.loginForm);
                // 判断返回的结果
                if(res.code == 200){// 登录成功
                    // 提示用户登录成功
                    this.$message.success("登录成功");
                    // 保存token
                    window.sessionStorage.setItem("token",res.data);
                    // 通过编程式导航跳转到后台主页，路由地址是home
                    this.$router.push("/home");
                }else{// 登录失败
                    // 提示账户或密码有误
                    this.$message.error("账户或密码有误!");
                }
             }else{
                // 不提交
                return;
             }
           });
           
        }
    }
}
</script>

<style>
    .login_container{
       background-color: #009688;
        height: 100%;
      
    }
    .login_box{
        width: 450px;
        height: 300px;
        background-color: #efef;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }
    .avatar_box {
        height:130px;
        width: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;
    }
   .avatar_box img {
       height:100%;
       width: 100%;
       border-radius: 50%;
       background-color: #eee;
   }
    .login_form{
        position: absolute;
        bottom: 0px;
        width: 100%;
        padding: 0px 20px;
        box-sizing: border-box;
    }
    .btns{
        display: flex;
        justify-content: flex-end;
    }
</style>
